package cn.webrx.coll;/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * <p>Project: jse2204 - cn.webrx.coll.ArrayListLinkedListDemo
 * <p>Powered by webrx On 2022-09-19 14:10:43
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class ArrayListLinkedListDemo {
    public static void main(String[] args) {
        long nn = 100000;
        long ee = 0;
        long ss = System.currentTimeMillis();


        System.out.println("添加元素效率：");
        LinkedList<String> list2 = new LinkedList<>();
        ss = System.currentTimeMillis();
        for (int i = 0; i < nn; i++) {
            list2.add(0, String.format("LinkedList-%d", i));
            //list2.add(String.format("LinkedList-%d", i));
        }
        ee = System.currentTimeMillis();
        System.out.printf("LinkedList:%d%n", ee - ss);


        ss = System.currentTimeMillis();
        ArrayList<String> list1 = new ArrayList<>();
        for (int i = 0; i < nn; i++) {
            list1.add(0, String.format("ArrayList-%d", i));
            //list1.add(String.format("ArrayList-%d", i));
        }
        ee = System.currentTimeMillis();
        System.out.printf("ArrayList:%d%n", ee - ss);

        System.out.println("遍历检索数据元素效率：");

        ss = System.currentTimeMillis();
        for(int i=0;i<nn;i++){
            String t = list2.get(i);
        }
        ee = System.currentTimeMillis();
        System.out.printf("LinkedList:%d%n", ee - ss);



        ss = System.currentTimeMillis();
        Iterator<String> it = list2.iterator();
        while(it.hasNext()){
            String t = it.next();
        }
        ee = System.currentTimeMillis();
        System.out.printf("Iterator-LinkedList:%d%n", ee - ss);



        List<String> list3 = new ArrayList<>(list2);
        for (int i = 0; i < nn; i++) {
            String t = list3.get(i);
        }
        ee = System.currentTimeMillis();
        System.out.printf("LinkedList->ArrayList:%d%n", ee - ss);




        ss = System.currentTimeMillis();
        for (int i = 0; i < nn; i++) {
            String t = list1.get(i);
        }
        ee = System.currentTimeMillis();
        System.out.printf("ArrayList:%d%n", ee - ss);


        //描述一下ArrayList  和 LinkedList 的区别？

    }
}
